import ajax from '../Utils/Ajax';
import tool from '../Utils/Tool';
import { SELECT_TAB, REQUEST_NEWS, RECEIVE_SLIDER, SLIDER_FAILED, RECEIVE_HOT, RECEIVE_GROUP, RECEIVE_GROUPITEM, RECEIVE_NEWS, RECORD_SCROLLT } from '../Constants/ActionTypes';

let actions = {

  selectTab: (tab) => ({
    type: SELECT_TAB,
    tab
  }),
  fetchSlider: () => (dispatch) => {
    let setting = {
      url: "phoneAppManager/getAppInfosByCode",
      data: { code: '600002', start: 0, iDisplayLength: 4 },
      callback: "succerSlider",
      error: function (res) {

      }
    }
    new ajax(setting);
    window.succerSlider = (res) => {
      res = tool.clear(res);
      res = tool.JSONParse(res);
      dispatch(actions.receiveSlider(res.ret, res.msg, res.data.totals, res.data.appModuleInfos))
    }
  },

  receiveSlider: (ret, msg, totals, sliderload) => ({
    type: RECEIVE_SLIDER,
    ret,
    msg,
    totals,
    sliderload
  }),

  sliderFailed: payload => ({
    type: SLIDER_FAILED,
    payload
  }),

  fetchHot: () => (dispatch) => {

    new ajax({
      url: "weixinApp/hotWeixin", data: {}, callback: "succerHot", error: function (res) {

      }
    });
    window.succerHot = function (res) {
      res = tool.clear(res);
      res = tool.JSONParse(res);
      dispatch(actions.receiveHot(res.data.result));
    }
  },

  receiveHot: (hotload) => ({
    type: RECEIVE_HOT,
    hotload
  }),

  fetchGroup: () => (dispatch) => {
    new ajax({
      url: "weixinApp/getClassify", data: {}, callback: "succerGroup", error: function (res) {

      }
    });
    window.succerGroup = function (res) {
      res = tool.clear(res);
      res = tool.JSONParse(res);
      dispatch(actions.receiveGroup(res.data.result));
    }
  },

  receiveGroup: (groupload) => ({
    type: RECEIVE_GROUP,
    groupload
  }),

  fetchGroupItem: (classify) => (dispatch) => {
    new ajax({
      url: "weixinApp/getWeixinByClassify", data: { classify: classify }, callback: "succerGroupItem", error: function (res) {

      }
    });
    window.succerGroupItem = function (res) {
      res = tool.clear(res);
      res = tool.JSONParse(res);
      dispatch(actions.receiveGroupItem(classify, res.data.result));
    }
  },

  receiveGroupItem: (classify, groupitemload) => ({
    type: RECEIVE_GROUPITEM,
    classify,
    groupitemload
  }),

  fetchNews: (tab, pageNo = 0, limit = 5) => (dispatch) => {
    // dispatch(actions.requestNews(tab));
    
    new ajax({
      url: "phoneAppManager/getAppInfosByCode", data: { code: "600008", start: pageNo, iDisplayLength: limit }, callback: "succerNews", error: function (res) {

      }
    });
    
    window.succerNews = function (res) {
      res = tool.clear(res);
      res = tool.JSONParse(res);
      console.log(res);
      dispatch(actions.requestNews(tab));
      dispatch(actions.receiveNews(tab, pageNo, limit, res.data.totals, res.data.appModuleInfos));
    }
  },

  requestNews: (tab) => ({
    type: REQUEST_NEWS,
    tab
  }),

  receiveNews: (tab, pageNo, limit, newstotals, newsload) => ({
    type: RECEIVE_NEWS,
    tab,
    pageNo,
    limit,
    newstotals,
    newsload
  }),

  recordScrollT: (tab,scrollT) => ({
    type: RECORD_SCROLLT,
    tab,
    scrollT
  })


}

export default actions;